package route

import (
	"SystemExploration/ability"
	"SystemExploration/controller"
	"github.com/gin-gonic/gin"
)

func Init() *gin.Engine {

	r := gin.Default()

	r.GET("/ping", func(c *gin.Context) {
		c.String(200, "pong")
	})

	//登录
	v1 := r.Group("/v1")
	{
		new(controller.Users).Router(v1)

	}
	//操作
	v1 = r.Group("/v1", routerMiddle())
	{
		//任务列表
		new(controller.Tasks).Router(v1)
		new(controller.Logs).Router(v1)

		//日志列表
	}
	return r
}

//自定义中间件第2种定义方式
func routerMiddle() gin.HandlerFunc {
	return func(c *gin.Context) {
		auto := c.GetHeader("auto")
		j := ability.NewJWT()
		dataInfo, err := j.ParserToken(auto)
		if err != nil {
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "认证过期",
			})
			c.Abort()
			return
		}
		c.Set("user", &dataInfo.Name)
		c.Next()
	}
}
